//
//  JddInitiatePlanViewController.m
//  PlayBall
//
//  Created by My Mac JiangDuoDuo on 14-8-8.
//
//

#import "JddInitiatePlanViewController.h"
#import "SchemeListTableViewCell.h"
#import "InitiatePlanDetailViewController.h"


#import "MJRefresh.h"

@interface JddInitiatePlanViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray *_schemeArr;
    UITableView *_tab;
    NSInteger _pageNo;
}

@end

@implementation JddInitiatePlanViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

//
- (void)loadNavBar
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    customLab.backgroundColor = [UIColor clearColor];
    [customLab setText:@"待发起订单"];
    customLab.textAlignment = NSTextAlignmentCenter;
    
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.font = [UIFont systemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
    //ios6 以下需要设置
//    //self.navigationItem.hidesBackButton = YES;
}


//
- (void)createTab{
    if (!_tab) {
        CGFloat tabHeight = SCREEN_HEIGHT;
        tabHeight = tabHeight-64;
        NSLog(@"height = %f",self.view.frame.size.height);
        _tab = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, tabHeight)
                                                    style:UITableViewStylePlain];
        [self setupRefresh];
    }
    _tab.dataSource = self;
    _tab.delegate = self;
    _tab.backgroundView = nil;
    _tab.userInteractionEnabled = YES;
    _tab.scrollEnabled = YES;
    _tab.backgroundColor = [UIColor clearColor];
    [_tab setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [_tab setSeparatorColor:SeparatorLineColor];
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    _tab.tableFooterView = v;

    if (IOS7) {
        [_tab setSeparatorInset:UIEdgeInsetsZero];
    }
    _pageNo = 0;
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_tab addHeaderWithTarget:self action:@selector(headerRereshing)];
#warning 自动刷新(一进入程序就下拉刷新)
    //[_tab headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tab addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    /*
    _tab.headerPullToRefreshText = @"下拉可以刷新了";
    _tab.headerReleaseToRefreshText = @"松开马上刷新了";
    _tab.headerRefreshingText = @"MJ哥正在帮你刷新中,不客气";
    
    _tab.footerPullToRefreshText = @"上拉可以加载更多数据了";
    _tab.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    _tab.footerRefreshingText = @"MJ哥正在帮你加载中,不客气";
     */
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    _pageNo = 1;
    [self getSchemeListRequest];
}

- (void)footerRereshing
{
    _pageNo++;
    [self getSchemeListRequest];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self loadNavBar];
    [self createTab];
}

- (void)viewWillAppear:(BOOL)animated{
    [MobClick beginLogPageView:@"DaiFaQiDingDan"];
    [MobClick event:@"UserCenter-daifaqidingdan"];
    [self setJddTabBarHidden:YES];
    _pageNo = 1;
    [self getSchemeListRequest];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"DaiFaQiDingDan"];
}

//获取待发起方案请求
- (void)getSchemeListRequest
{
    [JddCommonFunction setWaitingViewHidden:NO fromController:self];
    JddQueryInitiatePlanOperation *operation = [[JddQueryInitiatePlanOperation alloc] init];
    operation.delegate = self;
    operation.userId = [[JddGlobalParameter sharedInstance].dictionary objectForKey:GLOBAL_PARAMETER_USER_ID];
    operation.pageNo = [NSString stringWithFormat:@"%d",_pageNo];
    operation.pageSize = @"10";
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
}

//待发起方案服务器返回
- (void)InitiatePlanOperationResult:(NSData *)result
{
    [JddCommonFunction setWaitingViewHidden:YES fromController:self];
    if (result) {
        id info = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingMutableContainers error:nil];
        
        if (!_schemeArr) {
            _schemeArr = [[NSMutableArray alloc] init];
        }
        
        //[_schemeArr removeAllObjects];
        if ([info[@"Code"]intValue] == -1) {
            
        }else{
            NSLog(@"%@",[info[@"Data"]objectFromJSONString]);
            if ([[info[@"Data"]objectFromJSONString] objectForKey:@"item"]) {
                if ([[[info[@"Data"]objectFromJSONString] objectForKey:@"item"] count] > 0) {
                    
                    if (_pageNo == 1) {
                        [_schemeArr removeAllObjects];
                    }
                    [_schemeArr addObjectsFromArray:[[info[@"Data"]objectFromJSONString] objectForKey:@"item"]];
                }
            }
        }
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_tab headerEndRefreshing];
        [_tab footerEndRefreshing];
        
        [self.view addSubview:_tab];
        [_tab reloadData];
    }
}

#pragma mark UITableViewDelegate
#pragma mark -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //return [[_registerInfoDic objectForKey:[NSString stringWithFormat:@"userInfo%d",section]] count];
    return [_schemeArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"SchemeListTabStr";
    SchemeListTableViewCell* cell = (SchemeListTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell)
    {
        cell = [[SchemeListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr] ;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setPropertyInfo:[_schemeArr objectAtIndex:[indexPath row]]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([@"双色球" isEqualToString:[[_schemeArr objectAtIndex:[indexPath row]] objectForKey:@"LotteryID"]]) {
        if ([[[_schemeArr objectAtIndex:[indexPath row]] objectForKey:@"IsOverdue"] integerValue] == 1) {
            [self promptResInfo:@"此方案已过期"];
        }else{
            InitiatePlanDetailViewController *i = [[InitiatePlanDetailViewController alloc] init];
            i.schemeId = [[_schemeArr objectAtIndex:[indexPath row]] objectForKey:@"SchemeID"];
            [self.navigationController pushViewController:i animated:YES];
        }
    }else{
        [self promptResInfo:@"此功能尚在开发当中"];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
    return 0.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)promptResInfo:(NSString *)infoString
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] init];
    [hud setMode:MBProgressHUDModeText];
    hud.detailsLabelText = infoString;
    hud.detailsLabelFont = [UIFont systemFontOfSize:15];
    [self.view addSubview:hud];
    [hud show:YES];
    hud.margin = 10.f;
    hud.yOffset = -30.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1.0];
}

@end
